package com.jiayang.hhotel.presenter;

import android.app.AlertDialog;
import android.content.Context;
import android.support.annotation.IdRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RadioGroup;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.DivideAdapter;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.InfoModel;
import com.jiayang.hhotel.bean.LineRoomModel;
import com.jiayang.hhotel.bean.NowLiveDetailsModel;
import com.jiayang.hhotel.bean.RoomNumModel;
import com.jiayang.hhotel.net.Api;
import com.jiayang.hhotel.ui.NowLiveSearchDetailsActivity;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by Administrator on 2017/5/24 0024.
 * 现住查询详情页面逻辑类
 */

public class NowLiveSearchDetailsPresenter extends XPresent<NowLiveSearchDetailsActivity> {

    AlertDialog changeDialog;
    /**
     * 创建换房对话框
     * @param list 此处传递对象
     */
    public void createModifyRoomDialog(final Context context, List<RoomNumModel.PRsvforcastRoomDtoListBean> list){
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        View itemView = LayoutInflater.from(context).inflate(R.layout.dialog_modify_room,null);
        GridView roomListView = (GridView) itemView.findViewById(R.id.gv);
        Button deter = (Button) itemView.findViewById(R.id.bt_enterMine);
        final DivideAdapter adapter = new DivideAdapter(context,list,1);
        deter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!StringUtil.isEmpty(adapter.getRoomList())){
                    getV().clickDeter(adapter.getRoomList());
                }
            }
        });
        roomListView.setAdapter(adapter);
        builder.setView(itemView);
        changeDialog = builder.show();
    }

    AlertDialog LineRoomDialog;
    /**
     * 创建连房对话框
     * @param context 上下文
     */
    public void createLineRoomDialog(final Context context){
        final int[] index = {0};
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        View itemView= LayoutInflater.from(context).inflate(R.layout.dialog_line_room,null);
        RadioGroup gp = (RadioGroup) itemView.findViewById(R.id.rp);
        Button enterMine= (Button) itemView.findViewById(R.id.bt_enterMine);
        gp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i){
                    case R.id.rb_one://不转帐 0
                        index[0] = 0;
                        break;
                    case R.id.rb_two://公付 1
                        index[0] = 1;
                        break;
                    case R.id.rb_three://自付 2
                        index[0] = 2;
                        break;
                }
            }
        });
        enterMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    getV().clickLineRoomDeter(index[0]);
            }
        });
        builder.setView(itemView);
        LineRoomDialog =builder.show();
    }

    /**
     * 连房保存
     * @param mpLanguage 语言
     * @param mpOperation 状态0-当前；1-历史
     * @param mpRegister 序号
     * @param mpRoom 现住房号
     * @param mpType --0-不转账、1-公付账转本房、2-自付账转本房
     */
    public void LineRoomSave(int mpLanguage,int mpOperation,long mpRegister,String mpRoom,int mpType){
        Api.getHomeSer().LineHouse(Constant.PHONE,mpLanguage,mpOperation,mpRegister,mpRoom,mpType)
                .compose(XApi.<LineRoomModel>getApiTransformer())
                .compose(XApi.<LineRoomModel>getScheduler())
                .compose(getV().<LineRoomModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<LineRoomModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    @Override
                    public void onNext(LineRoomModel Model) {
                        LogUtil.showLog("连房请求="+Model.toString());
                        switch (Model.getState()){
                            case 9://请求成功
                                try{
                                    int result = Integer.valueOf(Model.getPReceptionLianfangDto().getResult());
                                    ToastUtil.Toast(getV().getString(R.string.line_room_finish),getV());
                                    LineRoomDialog.dismiss();
                                }catch (NumberFormatException e){
                                    ToastUtil.Toast(Model.getPReceptionLianfangDto().getResult(),getV());
                                }
                                break;
                        }
                    }
                });
    }

    /**
     * 获取现住详情
     * @param mpLanguage 语言
     * @param mpRegister 序号
     */
    public void getDetails(int mpLanguage,long mpRegister){
        Api.getHomeSer().getNowLiveDetails(Constant.PHONE,mpLanguage,mpRegister)
                .compose(XApi.<NowLiveDetailsModel>getApiTransformer())
                .compose(XApi.<NowLiveDetailsModel>getScheduler())
                .compose(getV().<NowLiveDetailsModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<NowLiveDetailsModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        CommonUtil.dismissProgressDialog(getV());
                        getV().sealPic(error.getType());
                    }

                    @Override
                    public void onNext(NowLiveDetailsModel Model) {
                        LogUtil.showLog("NowLiveDetailsModel="+Model.toString());
                        switch (Model.getState()){
                            case 9://请求成功
                                getV().setNowLiveDetailsModel(Model);
                                break;
                            case -1://失败
                                getV().sealPic(-1);
                                break;
                            case 1002://没有数据
                                LogUtil.showLog("现住详情没有数据");
                                break;
                        }
                    }
                });
    }

    /**
     * 换房确定按钮
     * @param mpLanguage 语言
     * @param mpRegister 序号
     * @param mpRoom 选择的房号
     */
    public void huanFangSave(int mpLanguage,long mpRegister,String mpRoom){
        Api.getHomeSer().HuanFangSave(Constant.PHONE,mpLanguage,mpRegister,mpRoom)
                .compose(XApi.<InfoModel>getApiTransformer())
                .compose(XApi.<InfoModel>getScheduler())
                .compose(getV().<InfoModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<InfoModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    @Override
                    public void onNext(InfoModel Model) {
                        LogUtil.showLog("换房按钮="+Model.toString());
                        switch (Model.getState()){
                            case 9://请求成功
                                changeDialog.dismiss();
                                break;
                            case -1://失败
                                break;
                        }
                    }
                });
    }

    /**
     * 保存编辑内容(暂时写到这里，在现住详情的编辑页中)
     * @param mpLanguage 语言
     * @param mpOperation 状态0-当前；1-历史
     * @param mpClerkAcct 编辑人帐号
     * @param mpForeignType 外国人类型（1-外国人；2-台湾同胞；3-港澳同胞；4-华侨）
     * @param mpKeepSecret 保密
     * @param mpVIP VIP
     * @param mpMemoryDate 重要
     * @param mpLangCode 语言
     * @param mpFancy 爱好
     * @param mpTaboo 禁忌
     * @param mpIllness 疾病
     * @param mpLivingHabit 睡眠习惯
     * @param mpDieteticHabit 饮食习惯
     * @param mpRoomService 客房服务要求
     * @param mpSpecial 特殊要求
     */
    public void saveEdit(int mpLanguage,int mpOperation,long mpClerkAcct,String mpForeignType,
                         int mpKeepSecret,String mpVIP,String mpMemoryDate,String mpLangCode,
                         String mpFancy,String mpTaboo,String mpIllness,String mpLivingHabit,
                         String mpDieteticHabit,String mpRoomService,String mpSpecial){
        Api.getHomeSer().hotelGuestInfoSave(Constant.PHONE,mpLanguage,mpOperation,mpClerkAcct
        ,mpForeignType,mpKeepSecret,mpVIP,mpMemoryDate,mpLangCode,mpFancy,mpTaboo,mpIllness,
                mpLivingHabit,mpDieteticHabit,mpRoomService,mpSpecial)
                .compose(XApi.<InfoModel>getApiTransformer())
                .compose(XApi.<InfoModel>getScheduler())
                .compose(getV().<InfoModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<InfoModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    @Override
                    public void onNext(InfoModel Model) {
                        switch (Model.getState()){
                            case 9://请求成功
                                break;
                            case -1://失败
                        }
                    }
                });
    }

    /**
     * 获取房号列表
     * @param mpLanguage 语言
     * @param mpArrDate 到店日期
     * @param mpDptDate 离店日期
     * @param mpRmUseType -房间使用形式（1-房租；2-钟点；3-床租；4-月租）
     * @param mpRmType 房间等级
     */
    public void initRoomList(int mpLanguage,String mpArrDate,String mpDptDate,int mpRmUseType,String mpRmType){
        Api.getHomeSer().getRoomNumList(Constant.PHONE,mpLanguage,1,mpArrDate,mpDptDate,mpRmUseType,mpRmType)
                .compose(XApi.<RoomNumModel>getApiTransformer())
                .compose(XApi.<RoomNumModel>getScheduler())
                .compose(getV().<RoomNumModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<RoomNumModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        LogUtil.showLog("error="+error);
                    }

                    @Override
                    public void onNext(RoomNumModel Model) {
                        switch (Model.getState()){
                            case 9://请求成功
                                getV().initDivideDialog(Model.getPRsvforcastRoomDtoList());
                                break;
                            case -1://失败
                                break;
                            case 1002://没有数据
                                ToastUtil.Toast("没有数据",getV());
                                break;
                        }
                    }
                });
    }

    /**
     * 改期
     * @param mpLanguage 语言
     * @param mpRegister 序号
     * @param mpDptDate 修改后日期
     */
    public void correctionDate(int mpLanguage,long mpRegister,String mpDptDate){
        Api.getHomeSer().correctionDate(Constant.PHONE,mpLanguage,mpRegister,mpDptDate)
                .compose(XApi.<InfoModel>getApiTransformer())
                .compose(XApi.<InfoModel>getScheduler())
                .compose(getV().<InfoModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<InfoModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    @Override
                    public void onNext(InfoModel Model) {
                        LogUtil.showLog("改期请求类="+Model.toString());
                        switch (Model.getState()){
                            case 9://请求成功
                                break;
                            case -1://失败
                                break;
                        }
                    }
                });
    }
}
